package com.talkweb.historylib.provider;

import com.talkweb.historylib.LargeImageAbility;
import com.talkweb.historylib.ResourceTable;
import com.talkweb.historylib.utils.ImageItemHelper;
import com.talkweb.securitycamera.common.constans.Constant;
import com.talkweb.securitycamera.common.interfaces.entity.IBaseFileEntity;
import com.talkweb.securitycamera.utils.DensityUtils;
import com.talkweb.securitycamera.utils.photocodeclib.PhotoDecode;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Image;
import ohos.media.image.PixelMap;

/**
 * @author SUQI
 * @date 2021/2/3
 * @description 嵌套item适配器
 **/
public class ImageItemProvider extends BaseListProvider<IBaseFileEntity> {
    private final static int PADDING_VP_10 = 10;
    private final int imageWidth;
    private final int padding;
    private Ability ctx;
    //当前列表第几行，从0开始
    private final int currentRow;

    public ImageItemProvider(Ability mContext, int currentRow) {
        super(mContext);
        this.currentRow = currentRow;
        padding = DensityUtils.vp2px(PADDING_VP_10);
        this.ctx = mContext;
        imageWidth = ImageItemHelper.getImageWidth(padding);
    }

    @Override
    protected void onBindViewHolder(ViewHolder viewHolder, IBaseFileEntity data, int position) {
        if (data == null) {
            return;
        }
        Image ivThumb = viewHolder.getView(ResourceTable.Id_iv_thumb);
        ivThumb.setWidth(imageWidth);
        ivThumb.setHeight(imageWidth);
        if (position > 0) {
            ivThumb.setMarginLeft(padding);
        }
        PixelMap pixelMap = new PhotoDecode.Builder().build().commonDecodePhoto(data.getFilePath());
        if (pixelMap != null) {
            ivThumb.setPixelMap(pixelMap);
        }
        ivThumb.setClickedListener(view -> {
            int realPosition = currentRow * ImageItemHelper.COLUMN_NUM + position;
            System.out.println("onBindViewHolder enter position=  " + realPosition);
            Intent intent = new Intent();
            intent.setParam(Constant.INIT_POSITION, realPosition);
            Operation op = new Intent.OperationBuilder().withAbilityName(LargeImageAbility.class.getName()).withBundleName("com.talkweb.securitycamera").build();
            intent.setOperation(op);
            ctx.startAbilityForResult(intent, Constant.DATA_CHANGED_CODE);
        });
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_item_view_nesting_image_list;
    }
}